/*
 * @Author: luthor luthor-li@qq.com
 * @Date: 2024-10-27 23:21:26
 * @LastEditors: luthor luthor-li@qq.com
 * @LastEditTime: 2024-10-27 23:21:40
 * @FilePath: \tilesethub_back\services\tokenService.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const jwt = require('jsonwebtoken')
const config = require('../config/config')

const generateToken = user => {
  const payload = {
    id: user.id,
    key: user.key,
    allowedTilesets: user.allowedTilesets
  }
  const token = jwt.sign(payload, config.JWT_SECRET, { expiresIn: '1h' })
  return token
}

const verifyToken = token => {
  try {
    const decoded = jwt.verify(token, config.JWT_SECRET)
    return decoded
  } catch (error) {
    throw new Error('Invalid token')
  }
}

module.exports = { generateToken, verifyToken }
